<script setup>
  import {ref} from "vue"
  
  // 导入axios用于发送请求
  import axios from "axios"

  // 1.定义响应式数组
  const newsList = ref([])

  // 2.在组件加载伊始发送ajax请求获取数据
  async function getNewsList() {
    const resp = await axios({
      url: 'http://localhost:4000/api/news',
      method: "GET"
    })
    console.log(resp)
    newsList.value = resp.data.data
  }
  getNewsList()
</script>

<template>
  <div class="item" v-for="item in newsList" :key="item.id">
    <div class="text">
      <p class="title">{{ item.title }}</p>
      <p><span class="source">{{ item.source }}</span><span class="source">{{ item.time }}</span></p>
    </div>
    <div class="pic">
      <img :src="item.img" :alt="item.title">
    </div>
  </div>
</template>

<style>
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }
  #app {
    width: 900px;
    margin: 50px auto;
  }
  .item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 200px;
    margin: 10px 0;
  }
  .text {
    height: 160px;
    width: 600px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
  .title {
    font-size: 20px;
    font-weight: bolder;
  }
  .source {
    margin-right: 2em;
    color: #bbb;
    font-size: 16px;
  }
</style>